<?php

class FormElement_Link extends FormElement
{

  /**
   * @var string
   */
  public $url;

  /**
   * @var string
   */
  public $labelField;

  /**
   * @var string
   */
  public $foreignKey;

  public function render($row, $action)
  {
    switch ($action) {
      case Form::ACTION_VIEW :
      case Form::ACTION_INDEX :
        $this->beforeRender($row, $action);

        $url = s::response()->makeUrl($this->url, array($this->getForeignKey() => $this->getFieldValue($row, $this->getField())));

        $output = '<a href="'.$url.'">'.$this->getValue($row).'</a>';

        $output = $this->afterRender($output, $row, $action);

        return $output;

        break;
    }

    return '';
  }

  protected function getValue(&$row)
  {
    if (! empty($this->labelField)) {
      return $row[$this->labelField];
    }

    return parent::getTitle();
  }

  public function injectQueryParams(&$params)
  {
    $params['fields'][] = $this->getField();

    if (! empty($this->labelField)) {
      $params['fields'][] = $this->labelField;
    }
  }

  public function getForeignKey()
  {
    if (empty($this->foreignKey)) {
      $this->foreignKey = $this->getField();
    }

    return $this->foreignKey;
  }

  public function setDataFromPost(&$row, $post, $action)
  {
  }

}

?>